package com.sgb.kjwl.viewmodel.self.carMakers;

import android.app.Application;
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.sgb.alimaplibrary.util.ToastUtil;
import com.sgb.capital.view.ui.activity.OpenAccountFirstActivity;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.view.ui.activity.transport.PayQuotationOrderForCompanyActivity;
import com.swgk.core.base.api.ICallBack;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.OrderEntity;
import com.sgb.kjwl.model.entity.self.MyOfferDetailsEntity;
import com.sgb.kjwl.model.entity.transport.AgreedCarOfferEntity;
import com.swgk.core.util.UserOperating;
import com.sgb.kjwl.view.ui.activity.transport.PayQuotationOrderActivity;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.dialog.RoundDialog;
import com.swgk.core.util.MToast;

import java.util.HashMap;
import java.util.Map;

/**
 * Describe:
 * Author:  yangLei
 * Date:    2021/4/7
 */
public class MyOfferDetailsModel extends AndroidViewModel {

    public MutableLiveData<MyOfferDetailsEntity> detailsLiveData = new MutableLiveData<>();

    public MyOfferDetailsModel(@NonNull Application application) {
        super(application);
    }

    /**
     * 检查车辆可使用状态
     */
    public void checkCarInfo(Context context, MyOfferDetailsEntity entity) {
        Map map = new HashMap();
        map.put("carNo", entity.getDriverNo());
        map.put("goodsSourceNo", entity.getSourceNo());
        DialogHelper.showProgressDialog(context, "加载中...");
        NewAPIManager.getInstance().getLogisticsAPI().checkCarStatues(map).enqueue(new ICallBack<BaseEntity<AgreedCarOfferEntity>>() {
            @Override
            public void onSuccess(BaseEntity<AgreedCarOfferEntity> data) {
                DialogHelper.dismissProgressDialog();
                if (data == null)
                    return;
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    if (data.getData() != null) {
                        AgreedCarOfferEntity offerEntity = data.getData();
                        if (TextUtils.isEmpty(offerEntity.getTerminal()))
                            return;
                        if (!offerEntity.isOpenFundsAccount() && offerEntity.getTerminal().equals("1")) {//货源货主企业没开通资金账户
                            new RoundDialog.Builder(context)
                                    .title("提示")
                                    .content("货主未开通收款账户，无法支付信息费，请联系货主开通资金账户")
                                    .confirm("确定")
                                    .singleBtn(true)
                                    .contentGravity(Gravity.CENTER)
                                    .cancelable(false)
                                    .show();
                        } else if (!offerEntity.isDriverOpenFundsAccount() && offerEntity.getTerminal().equals("2")) {//货源货主团队没开通资金账户
                            new RoundDialog.Builder(context)
                                    .title("提示")
                                    .content("货主未开通收款账户，无法支付信息费，请联系货主开通资金账户")
                                    .confirm("确定")
                                    .singleBtn(true)
                                    .contentGravity(Gravity.CENTER)
                                    .cancelable(false)
                                    .show();
                        } else if (!offerEntity.isDriverOpenFundsAccount() && offerEntity.getTerminal().equals("3")) {//个人货源资金账户
                            new RoundDialog.Builder(context)
                                    .title("提示")
                                    .content("开通在线收款账户可支付信息费")
                                    .confirm("确定")
                                    .singleBtn(true)
                                    .contentGravity(Gravity.CENTER)
                                    .cancelable(false)
                                    .show();
                        }
                        //检查自己
                        else if (!UserOperating.getInstance().getUserInfo().isDriverOpenFundsAccount()) {
                            new RoundDialog.Builder(context)
                                    .title("提示")
                                    .content("开通在线收款账户可支付信息费")
                                    .cancel("取消")
                                    .confirm("前往开户")
                                    .contentGravity(Gravity.CENTER)
                                    .cancelable(false)
                                    .confirmCallback(v -> OpenAccountFirstActivity.start(context, true))
                                    .show();
                            return;
                        } else {
                            //检查车辆状态
                            if (!TextUtils.isEmpty(offerEntity.getVehicleStatus())) {
                                if (TextUtils.isEmpty(offerEntity.getCarNumber())) {
                                    new RoundDialog.Builder(context)
                                            .title("车辆状态异常")
                                            .content("未获取到车辆信息")
                                            .confirm("确定")
                                            .singleBtn(true)
                                            .contentGravity(Gravity.CENTER)
                                            .cancelable(false)
                                            .show();
                                    return;
                                }

                                if (offerEntity.getVehicleStatus().equals("1")) {
                                    //被下架
                                    new RoundDialog.Builder(context)
                                            .title("车辆状态异常")
                                            .content(offerEntity.getCarNumber() + "已被下架，请至“用户中心 - 车辆管理”中重新提交认证")
                                            .confirm("确定")
                                            .singleBtn(true)
                                            .contentGravity(Gravity.CENTER)
                                            .cancelable(false)
                                            .show();
                                } else if (offerEntity.getVehicleStatus().equals("2")) {
                                    //被删除
                                    new RoundDialog.Builder(context)
                                            .title("车辆状态异常")
                                            .content(offerEntity.getCarNumber() + "已被系统删除，无法使用。更多问题，请联系客服：400-001-0051")
                                            .confirm("确定")
                                            .singleBtn(true)
                                            .contentGravity(Gravity.CENTER)
                                            .cancelable(false)
                                            .show();
                                } else if (offerEntity.getVehicleStatus().equals("3")) {
                                    //所有状态正常——>前去支付
                                    OrderEntity orderEntity = new OrderEntity();
                                    orderEntity.setGoodsSourceNo(entity.getSourceNo());
                                    orderEntity.setCarNumber(entity.getCarNumber());
                                    orderEntity.setHeightHurdle(entity.getHeightHurdle());
                                    orderEntity.setWeight(entity.getWeight());
                                    orderEntity.setCarLen(entity.getCarLenName());
                                    orderEntity.setCarType(entity.getCarTypeName());
                                    orderEntity.setCityName(entity.getCityName());
                                    orderEntity.setProvinceName(entity.getProvinceName());
                                    orderEntity.setDistrictName(entity.getDistrictName());
                                    orderEntity.setDetailAddress(entity.getDetailAddress());
                                    orderEntity.setDriverNo(entity.getDriverNo());
                                    orderEntity.setCarOfferNo(entity.getCarOfferNo());
                                    orderEntity.setGoodsOfferNo(entity.getGoodsOfferNo());
                                    orderEntity.setNote(entity.getNote());
                                    orderEntity.setDiyField(entity.getDiyField());
                                    orderEntity.setQuotation(true);
                                    orderEntity.setIsDump(entity.getIsDump());
                                    orderEntity.setInTime(entity.getInTime());
                                    if (!UserOperating.getInstance().isPersonal()) {
                                        PayQuotationOrderForCompanyActivity.start(context, entity.getGoodsOfferNo());
                                    } else {
                                        PayQuotationOrderActivity.start(context, entity.getGoodsOfferNo());
                                    }
                                }
                            }
                        }
                    }
                } else {
                    if (!TextUtils.isEmpty(data.getMsg()))
                        ToastUtil.show(context, data.getMsg());
                }
            }

        });
    }

    /**
     * 获取我的报价 详情信息
     */
    public void getOfferDetails(Context context, String offerNo) {
        NewAPIManager.getInstance().getLogisticsAPI().getOfferDetails(offerNo).enqueue(new ICallBack<BaseEntity<MyOfferDetailsEntity>>() {
            @Override
            public void onSuccess(BaseEntity<MyOfferDetailsEntity> data) {
                DialogHelper.dismissProgressDialog();
                if (data == null)
                    return;
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    detailsLiveData.postValue(data.getData());
                } else {
                    MToast.showToast(context, data.getMsg());
                }
            }
        });

    }
}
